package com.module.login.pop

import android.content.Context
import android.content.Intent
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.google.common.collect.Lists
import com.lxj.xpopup.XPopup
import com.module.app.base.popup.BottomPopupView
import com.module.app.bean.IType
import com.module.app.ext.loginFlow
import com.module.base.base.adapter.BaseAdapter
import com.module.base.base.adapter.BaseNewAdapter
import com.module.base.ext.activityResultFlow
import com.module.base.ext.observe
import com.module.base.utils.CommonUtils
import com.module.base.utils.LogUtils
import com.module.base.utils.RecycleViewUtil
import com.module.base.utils.ToastUtils
import com.module.login.R
import com.module.login.databinding.LoginPopLoginBinding
import com.module.login.model.LoginPhoneViewModel
import com.module.login.utils.UserConvertUtils
import com.module.third.ext.qqLoginSharedFlow
import com.module.third.ext.wxLoginSharedFlow
import com.module.third.qq.QQUtils
import com.module.third.wx.WxUtils

open class LoginPop(context: Context) :
    BottomPopupView<LoginPhoneViewModel, LoginPopLoginBinding>(context) {

    companion object {
        open fun show(context: Context) {
            val pop = XPopup.Builder(context)
                .hasShadowBg(true)
                .isDestroyOnDismiss(true)
                .asCustom(LoginPop(context)) as LoginPop
            pop.show()
        }
    }


    var recyclerview: RecyclerView? = null

    private val list: List<com.module.app.bean.ListData>
    private var isSelect = false

    private val qqUtils by lazy {
        QQUtils(lifecycle)
    }
    private val wxUtils by lazy {
        WxUtils(context, lifecycle)
    }


    private val adapter by lazy {
        object : BaseAdapter() {
            override fun onCreateDefaultViewHolder(
                parent: ViewGroup,
                viewType: Int
            ): com.module.app.adapter.holder.ListPopHolder {
                return com.module.app.adapter.holder.ListPopHolder(parent)
            }

        }
    }

    override fun bingViewModel() {
        mDatabind.vm = mViewModel
    }

    init {
        list = Lists.newArrayList<com.module.app.bean.ListData>()
        list.add(
            com.module.app.bean.ListData(
                1,
                CommonUtils.getString(R.string.login_wx_str),
                true
            )
        )
        list.add(
            com.module.app.bean.ListData(
                2,
                CommonUtils.getString(R.string.login_qq_str),
                true
            )
        )
    }

    override fun onCreate() {
        super.onCreate()
        recyclerview = findViewById(R.id.recyclerview)
        recyclerview?.layoutManager = RecycleViewUtil.getLinear(context)
        adapter.bindToRecyclerView(recyclerview)
        adapter.setOnItemClickListener(object :
            BaseNewAdapter.OnItemClickListener<com.module.app.adapter.holder.ListPopHolder, com.module.app.bean.ListData> {
            override fun onItemClick(holder: com.module.app.adapter.holder.ListPopHolder, bean: com.module.app.bean.ListData) {
                if (!isSelect) {
                    ToastUtils.showShort(R.string.login_checkbox_agreement_tips)
                    return
                }
                if (bean.type == 1) {
                    wxUtils.login()
                } else if (bean.type == 2) {
                    qqUtils.login()
                }
            }

        })
    }

     override fun initListener() {
        mDatabind.layoutCheckbox.setOnClickListener {
            onClickCheckBox()
        }
        observe(activityResultFlow) {
            onActivityResult(it.requestCode, it.resultCode, it.data)
        }
        //登录监听
        observe(loginFlow) {
            loginSuc()
        }
        //微信登录
        observe(wxLoginSharedFlow) {
            it?.let {
                mViewModel.login(
                    IType.ILogin.WX,
                    it.openid,
                    UserConvertUtils.convertWx(it)
                )
            }
        }
        //qq登录
        observe(qqLoginSharedFlow) {
            LogUtils.d("回调一次")
            it?.let {
                mViewModel.login(
                    IType.ILogin.QQ,
                    it.openid,
                    UserConvertUtils.convertQQ(it)
                )

            }
        }
    }


    override fun initData() {
        adapter.setNewData(list)
    }


    open fun onClickCheckBox() {
        isSelect = !isSelect
        mDatabind.ivCheckbox.setImageResource(if (isSelect) R.drawable.ic_checkbox_select else R.drawable.ic_checkbox)
    }

    private fun loginSuc() {
        dismiss()
    }

    open fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        qqUtils.onActivityResult(requestCode, resultCode, data)
    }

}
